﻿Public Class MySqlConfiguration
    Implements IConfig
    Public mysql_adress As String
    Public mysql_port As Integer = 3306
    Public mysql_database As String = "ewinsync"
    Public mysql_reset As Boolean
    Public mysql_user As String
    Public mysql_password As String
    Private validationerror As String
    Public Sub New()
        loadConfig()
    End Sub



    Public Function checkConfig() As Boolean Implements IConfig.checkConfig
        If (String.IsNullOrEmpty(mysql_adress)) Then
            validationerrors = noAdress()
            Return False
        End If
        If (mysql_port <= 0) Then
            validationerrors = noPort()
        End If
        If (String.IsNullOrEmpty(mysql_database)) Then
            validationerrors = noDb()
            Return False
        End If
        If (String.IsNullOrEmpty(mysql_user)) Then
            validationerrors = nouser()
            Return False
        End If
        Return True
    End Function

    Public Function loadConfig() As Boolean Implements IConfig.loadConfig
        mysql_adress = My.Settings.mysqlhost
        mysql_port = My.Settings.mysqlport
        mysql_database = My.Settings.mysqldb
        mysql_user = My.Settings.mysqluser
        mysql_password = My.Settings.mysqlpswd
        mysql_reset = My.Settings.resetmysql
        Return True
    End Function

    Public Property validationerrors As String Implements IConfig.validationerrors
        Get
            Return validationerror
        End Get
        Set(ByVal value As String)
            validationerror = value
        End Set
    End Property

    Public Function writeConfig() As Boolean Implements IConfig.writeConfig
        My.Settings.mysqlpswd = mysql_password
        My.Settings.mysqldb = mysql_database
        My.Settings.mysqlhost = mysql_adress
        My.Settings.mysqlport = mysql_port
        My.Settings.mysqluser = mysql_user
        My.Settings.resetmysql = mysql_reset
        Return True
    End Function

    Private Function noAdress() As String
        Dim cm As String = "MySql Server adress is not defined" + vbCrLf + "Do you want to use this setting " + vbCrLf + vbCrLf + "Select"
        cm = cm + vbTab + "- Cancel to revert the change"
        cm = cm + vbTab + "- No to correct value"
        cm = cm + vbTab + "- Yes to continue, but the value will be set to Locahost"
        Return cm
    End Function
    Private Function noPort() As String
        Dim cm As String = "The specified port is invalid for MySql Server" + vbCrLf + "Do you want to use this setting " + vbCrLf + vbCrLf + "Select"
        cm = cm + vbTab + "- Cancel to revert the change"
        cm = cm + vbTab + "- No to correct value"
        cm = cm + vbTab + "- Yes to continue, but the value will be set to 3306"
        Return cm
    End Function
    Private Function noDb() As String
        Dim cm As String = "MySql Server database name is not Specified" + vbCrLf + "Do you want to use this setting " + vbCrLf + vbCrLf + "Select"
        cm = cm + vbTab + "- Cancel to revert the change"
        cm = cm + vbTab + "- No to correct value"
        cm = cm + vbTab + "- Yes to continue, but the value will be set to ewinsync"
        Return cm
    End Function
    Private Function nouser() As String
        Dim cm As String = "MySql Server granted username is not specified" + vbCrLf + "Do you want to use this setting " + vbCrLf + vbCrLf + "Select"
        cm = cm + vbTab + "- Cancel to revert the change"
        cm = cm + vbTab + "- No to correct value"
        cm = cm + vbTab + "- Yes to continue, but the value will be set to root"
        Return cm
    End Function
End Class
